using System;
using System.Windows.Input;

namespace Wen.Core {

	public sealed class SimpleCommand : ICommand {
		private readonly Action _executeAction;
		private readonly Func<bool> _canExecutePredicate;

		public void Execute() {
			if (CanExecute())
				_executeAction();
		}

		void ICommand.Execute(object parameter) {
			Execute();
		}

		public bool CanExecute() {
			return _canExecutePredicate == null || _canExecutePredicate();
		}

		bool ICommand.CanExecute(object parameter) {
			return CanExecute();
		}

		public event EventHandler CanExecuteChanged {
			add { CommandManager.RequerySuggested += value; }
			remove { CommandManager.RequerySuggested -= value; }
		}

		public SimpleCommand(Action execute, Func<bool> canExecute = null) {
			if (execute == null)
				throw new ArgumentNullException("execute");
			_executeAction = execute;
			_canExecutePredicate = canExecute;
		}
	}

}